import re
import os


def search_file(file_name, encoding = "utf-8"):
    if not os.path.exists(file_name) :
        return 
    cn_pat = re.compile(u"([\u4e00-\u9fa5]+)")
    lines = open(file_name).readlines()
    cn_matches = []
    for row,line in enumerate(lines) :
        line = line.decode(encoding)
        results = cn_pat.findall(line)
        if results :
            cn_matches.append((row,line,results))
    if len(cn_matches) > 0 :
        print "file " + file_name + " got matches :"
        for row, line ,results in cn_matches :
            print "    " + str(row) + " : " + ",".join([item for item in results])


def search_dir(dir_name,file_types,encoding="utf-8"):
    if not os.path.exists(dir_name):
        return 
    for file_name in os.listdir(dir_name):
        file_path = os.path.join(dir_name, file_name)
        if os.path.isdir(file_path):
            search_dir(file_path,file_types,encoding)
        else :
            matched = False
            for file_type in file_types :
                if file_path.endswith(file_type):
                    matched = True
                    break
            if matched :
                search_file(file_path,encoding)

search_dir("/project/eiq/", [".py",".java"],"gbk")
